package com.interview.ujiuye;
public class Demo2  {
    private static class MessageLoop implements Runnable{
        @Override
        public void run() {
            try
            {
                for (int i = 0; i < 3; i++) {
                    System.out.println("-----------");
                    Thread.sleep(1000);
                    System.out.println(i);
                }
            } catch (InterruptedException e){
                System.out.println("A Exception:"+e.getMessage());
            }
        }
    }

    public static void main(String[] args) throws InterruptedException{
        long startTime = System.currentTimeMillis();
        Thread thread = new Thread(new MessageLoop());

        thread.start();
        System.out.println("X");
        while (thread.isAlive()){
            thread.join(1000);
            if ( (System.currentTimeMillis()-startTime) > 1000 && thread.isAlive() ){
                thread.interrupt();
                System.out.println("Y");
                thread.join();
                System.out.println("Z");
            }
        }
    }
}
